<?php

namespace App\Admin\Controllers\Data;

use App\Enums\OrderState;
use App\Models\Order;
use Dcat\Admin\Admin;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;

class OrderController extends AdminController
{
    protected string $title = '订单';

    public function grid(): Grid
    {
        $model = Order::ofAdmin()
            ->with(['season', 'chapter', 'player', 'gift', 'user', 'wechat', 'payment']);

        return Grid::make($model, function (Grid $grid) {
            $grid->disableCreateButton();
            $grid->disableActions();

            if (Admin::user()->isAdministrator()) {
                $grid->column('application.name', '所属应用');
            }

            $grid->quickSearch(['no'])
                ->placeholder('原始单号/支付单号');

            $grid->column('season.title', '赛季名称');
            $grid->column('chapter.title', '活动场次');
            $grid->column('player.name', '选手姓名');
            $grid->column('wechat.name', '微信名称');
            $grid->column('payment.name', '支付名称');
            $grid->column('gift.name', '礼物');
            $grid->column('qty', '数量');
            $grid->column('no', '订单编号')
                ->copyable();
            $grid->column('user_id');
            $grid->column('user.nickname', '支付用户');
            $grid->column('amount', '订单金额');
            $grid->column('state', '状态')
                ->using(OrderState::STATE_MAP)
                ->label(OrderState::STATE_LABEL);
            $grid->column('paid_at', '支付时间');
            $grid->column('created_at');
        });
    }
}
